var app = angular.module('ZJPC');

app.controller('ModalController', ['$scope', '$stateParams', '$state', '$http', 'DataService', function($scope, $stateParams, $state, $http, DataService) {

    var id=$stateParams.id;

    DataService.restGetApi('school/getAllTree').then(function (response){
      $scope.treeData = response.jiankong;
      var num = id.substr(3);
      getMonitor(num);
      $scope.treeData[setStatus(num)];
      $scope.treeConfig = {
            core : {
                multiple : false,
                animation: true,
                error : function(error) {
                    $log.error('treeCtrl: error from js tree - ' + angular.toJson(error));
                },
                check_callback : true,
                worker : true
            },
            types : {
                default : {
                    icon : 'glyphicon glyphicon-flash'
                },
                star : {
                    icon : '/img/gather.png'
                },
                cloud : {
                    icon : '/img/camera.png'
                }
            },
            version : 1,
            plugins : ['types','radio']
      };
    },function (err){
      console.log(err);
    });

    //点击不同点位树形图的展示
    function setStatus (id){
      if(id == '#'){
        return;
      }
      for(var i = 0; i < $scope.treeData.length; i ++){
        if($scope.treeData[i].id == id){
          $scope.treeData[i].state.opened = true;
          if($scope.treeData[i].type == 'cloud'){
            $scope.treeData[i].state.selected = true;
          }
          setStatus($scope.treeData[i].parent);
          return;
        }
      }
    }



    $scope.readyCB = function(e, m) {
      var id = m.node.original.id;
      console.log(id,9090);
      getMonitor(id);
    };

    function getMonitor (id){
      DataService.restGetApi('school/getByRegion?id=' + id)
        .then(function success(data){
            var previewXml = data.data.previewxml;

            var checked = $stateParams.name;

            $scope.startPlayView = function() {
              var OCXobj = document.getElementById("PlayViewOCX");
              console.log(previewXml);
              var ret = OCXobj.StartTask_Preview_InWnd(previewXml,0);
            }

            $scope.init = function(){
              var OCXobj = document.getElementById("PlayViewOCX");
              OCXobj.SetOcxMode(0);
              //设置监控窗口数量
              OCXobj.SetWndNum(1);
            }
            
            $scope.init();
            $scope.startPlayView();
        },function (err){
            console.log(err);
        })
    }

}]);